﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.Basic.Domain.T.Basic.Dto;
using T.Basic.Domain.T.Basic.Model;
using T.Basic.Read.API.Appliction.Command.CargoOwnerCommand;
using T.Basic.Read.API.Appliction.Command.LineCommand;

namespace T.Basic.Read.API.Controllers
{
    /// <summary>
    /// 读取线路信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]//JWT验证
    [ApiController]
    public class LinesController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public LinesController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示线路
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<ApiResult<PageDto<Line>>> GetLinesAll([FromQuery] LineListCommand command)
        {
            ApiResult<PageDto<Line>> result = new ApiResult<PageDto<Line>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据线路名称显示数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<ApiResult<List<Line>>> GetByLine([FromQuery]LineGetByCommand command)
        {
            ApiResult<List<Line>> result = new ApiResult<List<Line>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 导出线路文档
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<IActionResult> GetExcelName([FromQuery] LineListCommand command)
        {
            try
            {
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("线路列表");
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("编号");
                row.CreateCell(1).SetCellValue("线路名称");
                row.CreateCell(2).SetCellValue("起点");
                row.CreateCell(3).SetCellValue("终点");
                row.CreateCell(4).SetCellValue("是否是外协");
                row.CreateCell(5).SetCellValue("货主姓名");
                row.CreateCell(6).SetCellValue("货主手机号");
                row.CreateCell(7).SetCellValue("货主单位");
                row.CreateCell(8).SetCellValue("创建时间");
                var fueList = await _mediator.Send(command);
                int index = 1;
                foreach (var item in fueList.list)
                {
                    row = sheet.CreateRow(index);
                    row.CreateCell(0).SetCellValue(item.LineID);
                    row.CreateCell(1).SetCellValue(item.LineName);
                    row.CreateCell(2).SetCellValue(item.StartingProvince+item.StartingCity+item.StartingCounty+item.StartingPointAddress);
                    row.CreateCell(3).SetCellValue(item.EndProvince+item.EndCity+item.EndCounty+item.EndAddress);
                    row.CreateCell(4).SetCellValue(item.OutsourcedUnitsIsno==true?"是":"否");
                    row.CreateCell(5).SetCellValue(item.CargoOwnerName);
                    row.CreateCell(6).SetCellValue(item.CargoOwnerPhone);
                    row.CreateCell(7).SetCellValue(item.CargoOwnerUnitName);
                    row.CreateCell(8).SetCellValue(item.CreatedDate.ToString());
                    index++;
                }

                //导出
                byte[] buffer;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                string excelMine = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, excelMine, "线路列表.xlsx");

            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}
